<?php

namespace Modules\Production\Repository;


use App\Services\KingDeeService;
use Illuminate\Database\Query\Builder;
use Modules\Production\Models\StockTaskScan;

/**
 * 备货扫描记录
 * Class StockTaskScanRepository
 * @package Modules\Production\Repository
 */
class StockTaskScanRepository
{

    /**
     * 保存备货扫描记录
     * @param $data
     */
    static public function save($data){
        return (new StockTaskScan)->storeBy($data);
    }

    /**
     * 根据条件查询列表
     * @param $where
     * @return mixed
     */
    static public function getList($where){
        return StockTaskScan::where($where)->get();
    }

    /**
     * 查询未推送金蝶的备货扫描记录
     * @param $where
     * @return mixed
     */
    static public function getListNoKingdeeByProductionId($productionId){
        return StockTaskScan::where('order_id',$productionId)->where('push_kingdee',0)->get();
    }

    /**
     * 根据条件修改内容
     * @param $where
     * @param $data
     * @return mixed
     */
    static public function updateById($scanId,$data){
        return StockTaskScan::where('id',$scanId)->update($data);
    }

    /**
     * 查询已扫描数量
     * @param $where
     */
    static public function getScanCountByProductionAndMaterial($productionId,$materialNo){
        return StockTaskScan::where('order_id',$productionId)->where('push_kingdee',0)->where('material_no',$materialNo)->sum('qty');
    }

    /**
     * 根据箱号查询已扫描数量
     * @param $where
     */
    static public function getScanCountByLotnum($productionId,$materialNo,$lotnum,$location){
        return StockTaskScan::where('order_id',$productionId)
            ->where('material_no',$materialNo)
            ->where('push_kingdee',0)
            ->where('lot_num',$lotnum)
            ->where('location',$location)
            ->sum('qty');
    }

    /**
     * 获取工单领料条数
     * @param $productionId
     * @return mixed
     */
    static public function getCountByProductionId($productionId){
        return StockTaskScan::where('order_id',$productionId)->where('push_kingdee',1)->count();
    }
}
